/**
 * @author Guilherme Catarina
 * @since 04/07/2006
 */
 /**
  * Classe que exerce o controle de eventos.  */
import SCUMMCharacter.*;
import SCUMMScenario.*;

  
class SCUMMCore.Event
{
	/**
	 * 1 - Speak
	 * 2 - Walk
	 * 3 - Run an amimation	 */
	private var nCommand:Number;
	private var aText:Array;
	private var oActor:SCUMMActor;
	private var mcSprite:MovieClip;
	private var nX:Number;
	private var nY:Number;
	private var oScene:SCUMMScene;
	
	
	public function get text():Array {
		return this.aText;
	}
	public function set text(value:Array) {
		this.aText = value;
	}
	
	public function get actor():SCUMMActor {
		return this.oActor;
	}
	public function set actor(value:SCUMMActor) {
		this.oActor = value;
	}
	
	public function get sprite():MovieClip{
		return this.mcSprite;
	}
	public function set sprite(value:MovieClip) {
		this.mcSprite = value;
	}
	
	public function get command():Number {
		return this.nCommand;
	}
	public function set command( value:Number ):Void {
		this.nCommand = value;
	}
	
	public function get scene():SCUMMScene {
		return this.oScene;
	}
	
	public function Event( scene:SCUMMScene ) {
		this.oScene = scene;
	}
	
	public static function actorSpeak( text:Array, actor:SCUMMActor ):Event {
		var oEvent:Event = new Event();
		oEvent.nCommand = 1;
		oEvent.aText = text;
		oEvent.oActor = actor;
		return oEvent;
	}
	
	public static function actorWalkTo( actor:SCUMMActor, x:Number, y:Number ):Event {
		var oEvent:Event = new Event();
		oEvent.nCommand = 2;
		oEvent.oActor = actor;
		oEvent.nX = x;
		oEvent.nY = y;
		return oEvent;
	}
	
	public static function amination( sprite:MovieClip, scene:SCUMMScene ):Event {
		var oEvent:Event = new Event(scene);
		oEvent.nCommand = 3;
		oEvent.mcSprite = sprite;
		return oEvent;
	}
	
	public function run():Void {
		switch ( this.nCommand )
		{
			case 1:
				this.oActor.say(this.aText);
				break;
			case 2:
				this.oActor.walkTo(this.nX, this.nY);
				break;
			case 3:
				var oEvent:Event = this;
				this.mcSprite.onEnterFrame = function() {
					if ( this._currentframe == this._totalframes ) {
						oEvent.scene.endEvent();
						this.onEnterFrame = function() {};
					}
				};
				this.mcSprite.play();
				break;
			default:
				throw new Error("Event command invalid.");
		}
	}
	
	public function finishEvent():Void {
		switch ( this.nCommand )
		{
			case 1:
				this.oActor.state.skipCurrentPhase();
				break;
			case 2:
				this.oActor.state.skipCurrentPhase();
				break;
			case 3:
				this.mcSprite.gotoAndStop(this.mcSprite._totalframes);
				this.mcSprite.onEnterFrame = function() {};
				this.oScene.endEvent();
				break;
			default:
				throw new Error("Event command invalid: " + this.nCommand);
		}
	}
}